import * as TYPES from "@/constants/index";
import * as UTILS from "@/utils/index";

import api from "@/api/index";
import { Toast } from "vant";

const state = {
  ws: null
};

const mutations = {
  [TYPES.STORE_INIT_WEBSOCKET]: (state, payload) => {
    state.ws = payload;
  },
};

const actions = {
  //init websocket
  init: async ({ commit, dispatch, rootState, rootGetters }, payload) => {
    const { url } = payload;
    let ws = new WebSocket(url);
    ws.onopen = dispatch(TYPES.STORE_OPEN_WEBSOCKET);
    ws.onerror = dispatch(TYPES.STORE_ERROR_WEBSOCKET);
    ws.onmessage = dispatch(TYPES.STORE_MESSAGE_WEBSOCKET);
    ws.onclose = dispatch(TYPES.STORE_CLOSE_WEBSOCKET);
    commit(TYPES.STORE_INIT_WEBSOCKET, ws);
  },
  onopen: () => { },
  onerror: () => { },
  onmessage: () => { },
  onclose: () => { }

  
  
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
